<?php
namespace c\user;
\lang::load('user');

/**
 * Description of a
 *
 * @author YeFng
 */
class a extends \m\ad
{
    var $table = 'user';
    var $auth_m = 'user';
 
	var $title = 'User';
    
    var $State = array('disabled', 'enabled');
    
    function index_home()
	{
		$this->PU->check($this->auth_m);
        $this->title = \lang::g($this->title);
        $s = $this->P_G('s');
        
        //条件
        $where = '';
		if ($s['state']!='')
			$where .= ' AND state='. intval($s['state']);
        if ($s['au_group_id']!='')
			$where .= ' AND au_group_ids='. intval($s['au_group_id']);
         if ($s['grade_id']!='')
			$where .= ' AND grade_id='. intval($s['grade_id']);
        
		if ($s['name'])
		 {
		 	$_n = $this->db->escape($s['name']);
			$where .=" AND (name='$_n' or email='$_n' or phone='$_n' OR id='$_n')";
		 }
		$where && $where = substr($where, 4);

		$res = $this->db->get_page(array(), $this->table, '*', $where, 'id DESC');
        
		$this->loadTemplate('user/home', array('res'=>$res,
                                            'state'=>\lang::Ag($this->State),
                                            's'=>$s));
	}
    function index_post()
	{	
		$this->PU->check($this->auth_m);
		if ($id = intval($_GET['id']))
		 {
			$this->title = \lang::g('Edit') . \lang::g($this->title);	
            
            $data = $this->db->getOne($this->table, '*',  "id=$id");
            $data['expiry'] = arr_decode($data['expiry']);
		 }
		else{
			$this->title = \lang::g('Add') . \lang::g($this->title);
			$data = array('expiry'=>array(), 'state'=>1);
		}	

		$this->t_var['data'] = $data;
		$this->loadTemplate('user/post');
        $this->dispLayout = 'sub';
	}
	function index_save()
	{
        $this->PU->check($this->auth_m);
        $b = $this->save();
		ajax_json(is_array($b)?$b:\lang::g($b));
	}
    /**
     * 管理者操作保存
     * 
     * @param type $data
     * @param type $id
     * @return type  err
     */
	function save()
	{	
        $id = intval($_POST['data_id']);
		$data = $_POST['data'];
        $expiry = $_POST['expiry'];

        //auth_group
        $isAG = $this->PU->check('auth\group',  true);
        if ($isAG)
         {
            $data['au_group_ids'] = $data['au_group_ids'] ? intval_ids($data['au_group_ids']) : '';
            $expiry['au_group_ids'] = $expiry['au_group_ids'] ? intval_ids($expiry['au_group_ids']) :  '';
         }
        else
            unset($data['au_group_ids'], $expiry['au_group_ids']);
        
		if ($id)
		 {
		 	$where = 'id='. $id;
            unset($data['reg_area']);
		 }
		else{
			$where = null;
			$data['reg_area'] = getIP();
            $data['reg_time'] = date('YmdHis');
            
            if (!isset($data['au_group_ids']))
                $data['au_group_ids'] = '';
		 }      
         
        // grade
        if ($this->PU->check('user\grade',  true))
         {
            if (isset($data['grade_id']))
                $data['grade_id'] = intval($data['grade_id']);
         }
        else
            unset($data['grade_id']);
        
        //expiry
        if ($expiry['date'])
         {
            if ($expiry['per_group_ids'])
                $expiry['per_group_ids'] = intval_ids($expiry['per_group_ids']);
            $data['expiry'] = json_encode($expiry);
         }
        else
            $data['expiry'] = '';
        
		//name
        $data['name'] =$n= trim($data['name']);
        if (!$n or is_numeric($n{0}) or strpos($n, '@')!==false or strpos($n, '.')!==false)
            return 'user name wrongful';
        if ($this->db->getOne($this->table, 'name', 'name="'. $this->db->escape($data['name']) .'" AND id!='.$id ))
            return 'user already exist';
		//other name
		if ($data['phone'] && $this->db->getOne($this->table, 'name', 'phone="'. $this->db->escape($data['phone']) .'" AND id!='.$id ))
            return 'phone already exist other user';
		if ($data['email'] && $this->db->getOne($this->table, 'name', 'email="'. $this->db->escape($data['email']) .'" AND id!='.$id ))
            return 'email already exist other user';
        
        //pass
        if ($data['password']!='')
		 	$data['password'] = \YF_f::initC('\c\user\g')->password_ed($data['password']);
		else
			unset($data['password']);


        $data['birthdate'] or $data['birthdate'] = NULL;
        $data['parent_id'] = $data['parent_id']=='' ? 0 : \YF_f::initC('\c\user\g')->nameToID($data['parent_id']);
		if ($this->db->save($this->table, $data, $where))
		 {
		 	if ($id)
		 	 {
				\log::write($this->table, 'edit', $id);
		 	 }
			else{
				$id =  $this->db->insert_id();
                \log::write($this->table, 'add', $id);
			 }
            return array('data'=>$data, 'status'=>TRUE, 'id'=>$id);
		 }
		else
			return 'save fail';
	}
	function index_del()
	{
        $this->PU->check($this->auth_m);
        
        $ids = intval_ids($_GET['id']);
		$r = $this->db->getCol($this->table, 'id', "id IN($ids) AND online_time=0");
		if (!$r)
			ajax_json(\lang::g('prohibited operations'));
        $ids = implode(',', $r);
        
		$this->db->del($this->table, "id IN($ids)");
		if ($this->db->num_affected() > 0)
		  {
			\log::write($this->table, 'del', $ids);
			ajax_json(TRUE);
		  }
		ajax_json(\lang::g('Over'));
	}
}
